# -*- coding: utf-8 -*-
from PIL import Image, ImageDraw

ICON_SIZE = (81, 81)
BG_COLOR = (255, 255, 255, 0)
GRAY = (136, 136, 136)
BLUE = (0, 122, 255)

# 兼容Pillow 6.x，模拟线宽

def draw_poly(draw, points, color, width=4):
    for offset in range(-(width//2), width//2+1):
        shifted = [(x+offset, y+offset) for x, y in points]
        draw.polygon(shifted, outline=color)

def draw_rect(draw, box, color, width=4):
    x0, y0, x1, y1 = box
    for offset in range(-(width//2), width//2+1):
        draw.rectangle([x0-offset, y0-offset, x1+offset, y1+offset], outline=color)

def draw_ellipse(draw, box, color, width=4):
    x0, y0, x1, y1 = box
    for offset in range(-(width//2), width//2+1):
        draw.ellipse([x0-offset, y0-offset, x1+offset, y1+offset], outline=color)

# 图标绘制函数
def draw_home(draw, color):
    # 画房子
    draw_poly(draw, [(20, 45), (40, 25), (61, 45)], color)
    draw_rect(draw, [28, 45, 53, 65], color)
    draw.rectangle([38, 55, 43, 65], fill=color)

def draw_tree(draw, color):
    # 画树干
    draw.rectangle([38, 50, 43, 70], fill=color)
    # 画树冠
    draw_ellipse(draw, [26, 35, 56, 60], color)
    draw_ellipse(draw, [36, 25, 66, 55], color)

def draw_social(draw, color):
    # 画两个对话气泡
    draw_ellipse(draw, [18, 38, 48, 68], color)
    draw_ellipse(draw, [38, 23, 68, 53], color)
    draw.polygon([(28, 68), (32, 62), (38, 68)], fill=color)
    draw.polygon([(58, 53), (62, 47), (68, 53)], fill=color)

def draw_profile(draw, color):
    # 画头像
    draw_ellipse(draw, [28, 25, 53, 50], color)
    draw_ellipse(draw, [23, 50, 58, 70], color)

ICONS = [
    ("home", draw_home),
    ("tree", draw_tree),
    ("social", draw_social),
    ("profile", draw_profile),
]

for name, func in ICONS:
    for state, color in [("", GRAY), ("-active", BLUE)]:
        img = Image.new("RGBA", ICON_SIZE, BG_COLOR)
        draw = ImageDraw.Draw(img)
        func(draw, color)
        img.save("{}{}.png".format(name, state)) 